var fileData = '';

$(function (){

    $.ajax({
        cache : true,
        type : "post",
        url : "/jzweb/stuSignUpCom/cbatchList",
        data:{
            types: 3,
        },
        error : function(request) {
            parent.layer.alert("Connection error");
        },
        success : function(data) {

            if (data.code == 0) {
                let str = '';
                data.data?.map(function (v,k){
                    // if ( k == 0 ) str += `<option value="">请选择</option>`
                    str += `<option value="${v.id}">${v.name}</option>`
                })
                $("#selEbatchId").html(str);
            } else {
                parent.layer.alert(data.msg)
            }
        }
    });

})

    $("#practicalOperation").change(function (e){
        // var files = e.target.files;
        // var file0 = files[0];
        // formData.append('file', file0);
        var file = e.target.files[0];
        var reader = new FileReader();
        reader.onload = function(ev){
            var dataURL = ev.target.result;//'...(base64编码)...'
            fileData = dataURL;
        }



       reader.readAsDataURL(file);

    })

$("#but").click(function (){
    importComExcelFile($('#practicalOperation'),$('#signupForm'),$('#selEbatchId').val(),'/lottery/lotteryBinding')
})

//导入
function importComExcelFile(fileObj, formObj,cbatchId,url) {
        var fileUrl = fileObj.val();
        var suffix = fileUrl.substr(fileUrl.lastIndexOf(".")).toLowerCase();//获得文件后缀名
        if (suffix == '.xls' || suffix == '.xlsx') {
            if (fileUrl) {
                formObj.ajaxSubmit({
                    type: "post",
                    url: url,
                    data: {'cbatchId': cbatchId},
                    // header:{'Content-Type':'multipart/form-data'},
                    dataType: "json",
                    success: function (data) {
                        if (data.code == 0) {
                            parent.load();
                            parent.layer.msg("操作成功");
                            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                            parent.layer.close(index);
                        } else{
                            layer.msg(data.msg);
                        }
                    }, error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.log(XMLHttpRequest, textStatus, errorThrown)
                        layer.msg('出错了!');
                    }
                });
            } else {
                layer.msg('上传的文件不可为空');
                // parent.$.messager.alert('警告', "上传的文件不可为空", 'warning');
            }
        } else {
            layer.msg('请上传正确的Excel表格!');
            // $.messager.alert('警告', "", 'warning');
        }

}